import requests
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time

#水卡的用户信息（可以多个格式和下面的一样即可，变量名不能相同）
user={
    #用户名（手机号）
    "phone":"18888888888",

    #登录密码
    #由于密码加密问题（暂时不会），可以将登录密码改为aaa111111，下面则不用更改
    #或者自己抓登录包填写
    "pwd":"67BB234541",

    #用于每天接收余额信息的邮箱
    "receiver":"1212121212@qq.com"
}

#发送端邮箱的配置
conf={
    #发送信息的邮箱（用于给用户邮箱发送信息的邮箱）
    "sender_mail":"10086@qq.com",
    #邮箱的smtp授权码
    "auth_passport":"luzq12121212xnydjie",
    #邮箱服务器，qq的如下，其他的自行百度
    "server_path":"smtp.qq.com",
}


#登录函数
def login(phone,pwd):
    url="https://wfwapi.china-qzxy.cn/user/login"
    data={
        "password":pwd,
        "telPhone":phone,
        "openId":"",
        "typeId":"0",
        "phoneSystem":"android",
        "version":'6.0.6',
        "secretToken":"IhK+/@!,&*^%$#()"
    }
    headers={
        "User-Agent":"okhttp/3.12.4",
        "Host":"wfwapi.china-qzxy.cn"
    }
    resp = requests.post(url,data=data,headers=headers)
    ret = resp.json()
    token = ret["data"]["loginCode"]
    print(token)
    return token

#查询余额函数
def check(phone,token):
    url="https://wfwapi.china-qzxy.cn/wallet/money?telPhone="+phone+"&loginCode="+token+"&phoneSystem=android&version=6.0.6&secretToken=IhK%2B/%40%21%2C%26*%5E%25%24%23%28%29"
    print(url)
    headers={
        "Content-Type":"application/x-www-form-urlencoded;charset=UTF-8",
        "User-Agent":"okhttp/3.12.4",
        "Host":"wfwapi.china-qzxy.cn"
    }

    resp = requests.get(url,headers=headers)
    ret = resp.json()
    print(ret)
    money = ret["data"]["money"]
    # money = "111"
    print(money)
    return money

#发送邮箱函数
def sendmail(receivers,msg):
    try:
        subject="系统信息"
        server=smtplib.SMTP()
        server.connect(conf["server_path"])
        server.login(conf["sender_mail"],conf["auth_passport"])
        message=MIMEText(msg,"plain","utf-8")
        message["Subject"]=Header(subject,'utf-8')
        server.sendmail(conf["sender_mail"],receivers,message.as_string())
        server.quit()
        print("邮件发送成功")
    except smtplib.SMTPException:
        print ("Error: 无法发送邮件")
    pass

#主要流程函数
def do_req(user):
    token = login(user["phone"],user["pwd"])
    money=check(user["phone"],token)
    time_tuple = time.localtime(time.time())
    now_time = "{}年{}月{}日{}点{}分{}秒".format(time_tuple[0],time_tuple[1],time_tuple[2],time_tuple[3],time_tuple[4],time_tuple[5])
    receiver = user["receiver"]
    msg = "水卡用户: "+user["phone"]+"\n当前余额: "+money+"元\n"+"查询时间: "+now_time
    sendmail(receiver,msg)
    pass

#主函数
def main_handler(event, context):
    #多个用户下面添加函数即可 如：do_rep(user2)
    do_req(user)
    return("发送成功")
